package com.zhichat.web.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.zhichat.dao.AdminUserDao;
import com.zhichat.dao.UserInfoDao;
import com.zhichat.entity.AdminUserEntity;
import com.zhichat.infrastructure.redis.Keys;
import com.zhichat.infrastructure.redis.RedisRepository;
import com.zhichat.infrastructure.redis.rediskey.Key;
import com.zhichat.infrastructure.redis.rediskey.ZhiChat;
import com.zhichat.infrastructure.util.Utils;
import com.zhichat.web.api.R;
import com.zhichat.web.vo.AdminUserInfoVO;
import com.zhichat.web.vo.CheckCodeVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.HashMap;
import java.util.List;

/**
 * 辅助功能
 *
 * @Author: insanetz https://space.bilibili.com/602197686
 * @CreateDate: 2024/11/18 21:31
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/assist")
public class AssistController {

    private final RedisRepository redisRepository;

    /**
     * 获取验证码
     */
    @PostMapping("/checkCode")
    public R<CheckCodeVO> checkCode() {
        CircleCaptcha lineCaptcha = CaptchaUtil.createCircleCaptcha(100, 42, 4, 20);
        // 获取运算的结果
        String code = lineCaptcha.getCode();
        String captchaBase64 = lineCaptcha.getImageBase64Data();
        // 唯一编码，用于前后端校验
        String checkCodeUniq = Utils.uniqueId();
        // 存入 redis 设置 1分钟有效
        Key key = new ZhiChat(Keys.CHECK_CODE, Duration.ofMinutes(1));
        redisRepository.set(key, code, checkCodeUniq);
        CheckCodeVO checkCodeVO = new CheckCodeVO(checkCodeUniq, captchaBase64);
        return R.success(checkCodeVO);
    }

    private final RedisTemplate<String, Object> redisTemplateString;

    @GetMapping("t1")
    public R<String> t1() {
        Long myId = 1878045221258612736l;

        Long userId = 1879130727505522688l;
        Long userId1 = 1879130727505522689l;
        Long userId2 = 1879130727505522687l;

        Long userId3 = 1879130727505522686l;

        Key key = new ZhiChat(Keys.CONTACT_USER);
//        redisRepository.addAll(key, Lists.newArrayList(userId,userId1,userId2), myId);


        redisRepository.add_1(key, Sets.newHashSet(userId3), myId);
//        redisRepository.sRemove(key, Sets.newHashSet(userId3),myId);

//
//
//        System.out.println(isMember);
//
//        System.out.println(redisTemplateString.opsForSet().isMember(key.getKey(myId), userId));
//
//        List<String> members = redisRepository.members(key, String.class, myId);
//        System.out.println(JSON.toJSONString(members));
//
//        List<Long> members1 = redisRepository.members(key, Long.class, myId);
//        System.out.println(JSON.toJSONString(members1));


        return R.success("t1");
    }

    private final AdminUserDao adminUserDao;

    private final UserInfoDao userInfoDao;

    @GetMapping("t2")
    public R<String> t2() {
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("number", 2);
        param.put("size", 2);
        List<AdminUserInfoVO> demo = userInfoDao.demo(param);
        System.out.println("结果集 - " + JSON.toJSONString(demo));
        System.out.println("---------------------------------------------------------");


        List<AdminUserEntity> paramList = Lists.newArrayList();
        AdminUserEntity a = new AdminUserEntity();
        a.setPhone("123");
        AdminUserEntity a1 = new AdminUserEntity();
        a1.setPhone("321");
        AdminUserEntity a2 = new AdminUserEntity();
        a2.setPhone("456");
        paramList.add(a);
        paramList.add(a1);
        paramList.add(a2);
//        int maps = adminUserDao.batchSelect(paramList);
//        System.out.println(JSON.toJSONString(maps));
        return R.success("t2");
    }
}
